import { defineStore } from 'pinia';
import axios from 'axios';
// import { post } from 'node_modules/axios/index.cjs';
// import {request} from '../request/api'
// import qs from 'qs'

interface Message {
  id: number;
  text: string;
  isUser: boolean;
}

export const useChatStore = defineStore('chatStore', {
  state: () => ({
    messages: [] as Message[],
    messageId: 1,
  }),
  actions: {
    addUserMessage(text: string) {
      this.messages.push({
        id: this.messageId++,
        text,
        isUser: true,
      });
    },
    addAiMessage(text: string) {
      this.messages.push({
        id: this.messageId++,
        text,
        isUser: false,
      });
    },
    fetchAiResponse(userInput: string) {
      // request.chat()
      console.log('发送请求中，用户输入:', userInput); // 请求前日志
      let data = {"data":userInput}
      axios.post('http://127.0.0.1:5000/chat', data, {
  headers: {
    'Content-Type': 'application/json',
    'Origin': 'http://localhost:5173'
  }
})
.then((res) => {
  console.log('API 响应:', res); // 响应日志
  const aiResponse = res.data.data;
  this.addAiMessage(aiResponse);
})
.catch((error) => {
  console.error('API 调用失败:', error);
  this.addAiMessage('AI 助手暂时不可用，请稍后再试。');
});
      
    },
  },
});